package game_controller_config;

import g4p_controls.G4P;
import g4p_controls.GButton;
import g4p_controls.GCScheme;
import g4p_controls.GEvent;
import g4p_controls.GLabel;

import java.util.ArrayList;
import java.util.List;

import org.gamecontrolplus.ControlDevice;
import org.gamecontrolplus.ControlIO;

import processing.app.Editor;
import processing.core.PApplet;

public class TSelectPApplet extends PApplet {

	public int appWidth, appHeight;

//	static Editor editor;

	ControlIO controlIO;
	GLabel lblControls; 
	GButton btnExit;

	List<TSelectEntry> deviceEntries =  new ArrayList<TSelectEntry>();

	public void setup() {
		size(appWidth, appHeight, JAVA2D);
		G4P.messagesEnabled(false);                                                                             
		G4P.setGlobalColorScheme(GCScheme.GREEN_SCHEME);
		createSelectionInterface();
		registerMethod("dispose", this);

	}

	public void initControlIO(){
		controlIO = ControlIO.getInstance(this);
		List<ControlDevice> devices = controlIO.getDevices();
		// Add entries for devices added
		for(ControlDevice d : devices){
			if(d != null && !d.getTypeName().equalsIgnoreCase("keyboard"))
				deviceEntries.add(new TSelectEntry(this, controlIO, d));
		}
		// Sort entries and reposition on screen
		for(int i = 0; i < deviceEntries.size(); i++)
			deviceEntries.get(i).setIndex(20, i);

	}


	public void createSelectionInterface(){
		lblControls = new GLabel(this, 0, 0, width, 20);
		lblControls.setText("Game Devices Available");
		lblControls.setOpaque(true);
		lblControls.setTextBold();
		btnExit = new GButton(this, width - 105, height-28, 100, 24);
		btnExit.setText("Exit Tool");
		btnExit.addEventHandler(this, "exitClick");
	}

	public void exitClick(GButton source, GEvent event) {
		if(btnExit == source && event == GEvent.CLICKED) {
			exit();
		}
	}

	public void dispose(){
		System.out.println("Disposing");
		for(TSelectEntry entry : deviceEntries){
			if(entry.winCofig != null){
				entry.winCofig.close();
				entry.winCofig = null;
			}
		}
	}

	public void draw(){
		background(255, 255, 220);
		noStroke();
		stroke(230, 230, 200);
		fill(240, 240, 210);
		int y = 40;
		while(y < height){
			rect(0,y,width,20);
			y += 40;
		}
		fill(200,255,200);
		rect(0,height-30,width,30);
	}

}
